package sockettest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 
 * @author ZZWang
 * @Time 2017年9月14日  上午11:10:02
 */
public class SocketServer {

	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(8080);
		while (true) {
			Socket socket = serverSocket.accept();
			//接收消息
			InputStream input = socket.getInputStream();
			InputStreamReader reader = new InputStreamReader(input, "utf-8");
			BufferedReader bufferRea = new BufferedReader(reader);
			String data;
			while ((data = bufferRea.readLine()) != null) {
				System.out.println("我是服务器，客户端说：" + data);
			}
			// 关闭输入流
			socket.shutdownInput();
			
			// 发送消息
			OutputStream output = socket.getOutputStream();
			OutputStreamWriter writer = new OutputStreamWriter(output,"utf-8");
			BufferedWriter bufferWri = new BufferedWriter(writer);
			bufferWri.write("欢迎您！");
			bufferWri.flush();
			
			bufferWri.close();
			writer.close();
			output.close();
			bufferRea.close();
			reader.close();
			input.close();
			socket.close();
			serverSocket.close();
		}
	}
}
